/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package framework;

import java.util.Hashtable;
import java.util.Map;

/**
 *
 * Filter that drops fields from a frame in specified positions (passed to the constructor).
 * 
 */
public class Dropper extends FilterFramework {

    /**
     * represents all the ids of the measurements that should be dropped from the pipe.
     */
	private Map<Integer, Integer> positionsToDrop;

    /**
     *
     * Constructor
     *
     * @param posToDrop the ids of the measurements that should be dropped from the pipe.
     */
    public Dropper(int[] posToDrop) {
        super(1, 1);

        positionsToDrop = new Hashtable<Integer, Integer>();
        for (int i = 0; i < posToDrop.length; i++) {
            positionsToDrop.put(posToDrop[i], posToDrop[i]);
        }

    }

	/**
	 * 
	 * This method connects one filter to the output port of this filter
	 * 
	 * @param Filter
	 *            The filter that we want to connect to this filter
	 * @param OutputPortIndex
	 *            The pipe port index where we want to connect the filter.
	 */
    public void Connect(FilterFramework Filter, int OutputPortIndex) {
        super.Connect(Filter, 0, OutputPortIndex);
    }

    
    /**
	 * The main method of the filter. Starts when the thread is launched
	 */
    @Override
    public void run() {

        try {

            while (true) {
                int id = this.ReadInt(0);

                byte[] longValBytes = new byte[8];
                for (int i=0; i<8; i++ )
                {
                    longValBytes[i] = ReadFilterInputPort(0);
                }
                
                if (!positionsToDrop.containsKey(id)) {
                    writeInt(id, 0);

                    this.WriteFilterOutputPort(longValBytes, 0);
                }
            }
        } catch (EndOfStreamException e) {
            ClosePorts();
            System.out.print("\n" + this.getName() + "::Dropper Exiting;");
        } // catch
        catch (Exception e) {
            ClosePorts();
            System.out.print("\n" + this.getName() + "::ERROR::Dropper Exiting;" + e.getMessage());
        } // catch
    }
}

